
$(function(){
    $("#UpUser").dialog({
        title:"修改用户",
        btnName:"修改",
        btnBind:function(){
            var params=$("#updateUser").serializeArray();
            var param="";
            for(var i in params){

                if(i==params.length-1){
                    param+=params[i].name+"="+params[i].value;
                }else{
                    param+=params[i].name+"="+params[i].value+"&";
                }
            }
            console.info(param)


            $.post("sys/user/updateUser",param,function (msg) {
                $("#UpUser").dialog("close");
                $("#table").bootstrapTable("refresh");
                toastr.warning("信息提示",msg);
            },"text")
        }
    });

    $("#AddUser").dialog({
        title:"添加用户",
        btnName:"添加",
        btnBind:function(){
            var params=$("#adUser").serializeArray();
            var param="";
            for(var i in params){

                if(i==params.length-1){
                    param+=params[i].name+"="+params[i].value;
                }else{
                    param+=params[i].name+"="+params[i].value+"&";
                }
            }
            console.info(param)


            $.post("sys/user/addUser",param,function (msg) {
                $("#AddUser").dialog("close");
                $("#table").bootstrapTable("refresh");
                toastr.warning("信息提示",msg);
            },"text")
        }
    });
});

function FormatState(val,row,index) {
    if(val=='y'){
        return "可登陆";
    }else{
        return "不可登录";
    }
}

function FormatRole(val,row,index) {
    var name=null;
    $.ajax({
        url:'sys/role/searchByRole',
        data:"rid="+val,
        dataType:'json',
        type:'post',
        async:false,
        success:function (result) {
            name= result.roleName;
        }
    });
    return name;
}

function FormatOperation(value,row,index){
    var peration = "<span class='btn btn-default' onclick='updateUser("+JSON.stringify(row)+")'><i class='glyphicon glyphicon-edit'></i>修改</span>&nbsp;<span class='btn btn-warning' onclick='deleteUser("+row.userId+")'><i class='glyphicon glyphicon-remove'></i>删除</span>";
    return peration;
}

function addUser() {
    $(".modal .modal-body #AddUser").attr("class","show");
    $("#AddUser").dialog("open");
}

function  updateUser(row) {
    $("#updateUser input[name='loginName']").val(row.loginName);
    $("#updateUser input[name='password']").val(row.password);
    $("#updateUser input[name='userName']").val(row.userName);
    $("#updateUser input[name='userId']").val(row.userId);
    $("#updateUser input[name='hospital']").val(FormatHospital(row.hospitalCode));
    $("#updateUser input[name='branch']").val(FormatBranch(row.branchCode));
    $("#updateUser input[name='department']").val(FormatDepartment(row.departmentCode));
    $("#updateUser select[name='roleCode']").val(row.roleCode);
    $("#updateUser select[name='loginFlag']").val(row.loginFlag);
    $("#updateUser input[name='email']").val(row.email);
    $("#updateUser input[name='phoneNo']").val(row.phoneNo);
    $("#updateUser input[name='mobileNo']").val(row.mobileNo);
    $("#updateUser textarea[name='remarks']").val(row.remarks);




    $(".modal .modal-body #UpUser").attr("class","show");
    $("#UpUser").dialog("open");
    console.info(row);
}

function deleteUser(uid) {
    var param="uid="+uid;
    $.post("sys/user/delUser",param,function (msg) {
        toastr.warning(msg);
        $("#table").bootstrapTable("refresh")
    },"text")
}
function searchTjUser() {
    var param=$("#condition").serializeArray();
    console.info(param)

    var obj={};
    for(var i in param){
        obj[param[i].name]=param[i].value;
    }
    $("#table").bootstrapTable("refresh",{query:obj});

}

function FormatHospital(val,row,index){
    var name=null;
    $.ajax({
        url:'sys/hospital/searchByHospital',
        data:"id="+val,
        dataType:'json',
        type:'post',
        async:false,
        success:function (result) {
            name= result.hospitalName;
        }
    });
    return name;
}
function FormatBranch(val,row,index){
    var name=null;
    $.ajax({
        url:'sys/branch/searchByBranch',
        data:"id="+val,
        dataType:'json',
        type:'post',
        async:false,
        success:function (result) {
            name= result.branchName;
        }
    });
    return name;
}

function FormatDepartment(val,row,index){
    var name=null;
    $.ajax({
        url:'sys/department/searchByDepartment',
        data:"id="+val,
        dataType:'json',
        type:'post',
        async:false,
        success:function (result) {
            name= result.departmentName;
        }
    });
    return name;
}

/*
function prepareGrant() {
    var rows=$("#table").bootstrapTable("getSelections");
    if(rows.length==0){
        swal("请选择一项授权！");
        return;
    }
    var param="uid="+rows[0].id;
    $.getJSON("sys/user/tree",param,function(result){
        $("#UR").treeview({
            data:result,
            multiSelect:true,
            showCheckbox:true
        })
        $("#UR").dialog("open");
    })

}*/
